package cn.yh.register.hosp.controller;

import cn.yh.register.common.exception.RegisterException;
import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.MD5;
import cn.yh.register.hosp.service.HospitalSetService;
import cn.yh.register.model.hosp.HospitalSet;
import cn.yh.register.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    HospitalSetService hospitalSetService;

    // 查询所有医院设置信息
    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    // 删除医院设置
    @ApiOperation("根据id删除医院设置")
    @ApiImplicitParam(name = "id",value = "医院设置的id",required = true,dataType = "Long",paramType = "path")
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 带分页的条件查询
    @ApiOperation("带分页的条件查询")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findPageHospSet(@PathVariable Integer current,
                                             @PathVariable("limit") Integer size,
                                             @RequestBody(required = false) HospitalSetQueryVo queryVo){
        // 构造Page对象
        Page<HospitalSet> page = new Page<>(current, size);
        // 构造查询条件
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        // 名称模糊查询 编号精确查询
        wrapper.like(!StringUtils.isEmpty(queryVo.getHosname()), HospitalSet::getHosname, queryVo.getHosname())
                .eq(!StringUtils.isEmpty(queryVo.getHoscode()), HospitalSet::getHoscode, queryVo.getHoscode());
        Page<HospitalSet> hsPage = hospitalSetService.page(page, wrapper);
        return Result.ok(hsPage);
    }

    // 添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result<?> saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        // 设置状态 1：启用  0：禁用
        hospitalSet.setStatus(1);
        // 设置签名key  系统毫秒值+1000以内的随机数进行MD5加密
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + new Random(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 根据id查询医院设置
    @ApiOperation("根据id查询医院设置")
    @ApiImplicitParam(name = "id",value = "主键id",required = true,dataType = "Long",paramType = "path")
    @GetMapping("/getHospSet/{id}")
    public Result<HospitalSet> findById(@PathVariable Long id){
        try {
            int i = 10 / 0;
        } catch (Exception e){
            throw new RegisterException("不能除以0哦~", 20001);
        }

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    // 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @ApiImplicitParam(name = "hospitalSet",value = "医院设置信息",required = true,dataType = "HospitalSet",paramType = "body")
    @PostMapping("/updateHospitalSet")
    public Result<?> updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result<?> batchRemove(@RequestBody List<Long> idList){
        boolean flag = hospitalSetService.removeByIds(idList);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 解锁/锁定医院设置
    @ApiOperation(value = "锁定/解锁医院设置")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result<?> lockHospitalSet(@PathVariable Long id, @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 设置状态
        hospitalSet.setStatus(status);
        // 修改
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    // 发送签名key
    @ApiOperation(value = "发送签名key")
    @PutMapping("/sendKey/{id}")
    public Result<?> sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 取医院code 签名key
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        // 发送短信
        // ...
        return Result.ok();
    }

}
